﻿using System;
using System.IO;
using System.Windows.Forms;
using Twitterizer;

namespace melodyMe
{
    public partial class PreferencesForm : Form
    {
        private string requestToken;
        public PreferencesForm()
        {
            InitializeComponent();

            //They've already accepted the disc. so disable the button. Thanks to @_Logie for the bugnotice!
            if (Properties.Settings.Default.DL_Disclaimer) { btnAcceptDisclaimer.Enabled = false; }
        }

        private void btnClearPlaylist_Click(object sender, EventArgs e)
        {
            //Removes defaultPlaylist from the musicMe APPDATA folder.
            string mmPLF = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\melodyMe\\Playlists\\";

            if(Directory.Exists(mmPLF))
            {
                try
                { Directory.Delete(mmPLF); }
                catch
                { MessageBox.Show("Failure to delete Playlist Data. Please try again."); }
            }

            System.Media.SystemSounds.Asterisk.Play();
            MessageBox.Show("Changes will be applied after a restart.", "melodyMe");

        }

        private void btnRemoveAllData_Click(object sender, EventArgs e)
        {
            //Removes the musicMe Application Data folder, ergo all the files within.
            string mmDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\melodyMe";

            if (Directory.Exists(mmDir))
            {
                try
                { Directory.Delete(mmDir, true); }
                catch
                { MessageBox.Show("Failure to delete melodyMe folder. Please try again."); }
            }

            System.Media.SystemSounds.Asterisk.Play();
            MessageBox.Show("Changes will be applied after a restart.", "melodyMe");
        }

        private void btnAcceptDisclaimer_Click(object sender, EventArgs e)
        { 
            btnAcceptDisclaimer.Enabled = false;
            Properties.Settings.Default.DL_Disclaimer = true;
            Properties.Settings.Default.Save();
            MessageBox.Show("Changes will be applied after a restart.", "melodyMe");
        }

        private void btnStage1Auth_Click(object sender, EventArgs e)
        {
            try
            {
                OAuthTokenResponse requestAccessTokens = OAuthUtility.GetRequestToken(
                    Properties.Settings.Default.consumer_key,
                    Properties.Settings.Default.consumer_secret,
                    "oob");

                this.requestToken = requestAccessTokens.Token;

                //Route user to Twitter Web to get Pin Code.
                string authorUri = OAuthUtility.BuildAuthorizationUri(requestAccessTokens.Token).AbsoluteUri;
                TwitterClient webAuth = new TwitterClient(authorUri);
                webAuth.ShowDialog();

                //Get Access Tokens using cons, reqToken and new PIN Code!
                OAuthTokenResponse accessTokens = OAuthUtility.GetAccessToken(
                           Properties.Settings.Default.consumer_key,
                           Properties.Settings.Default.consumer_secret,
                           this.requestToken,
                           webAuth.PINCODE);

                //Get persistent token, save it.
                //Reload Settings for current program instance.
                Properties.Settings.Default.access_token = accessTokens.Token;
                Properties.Settings.Default.access_secret = accessTokens.TokenSecret;
                Properties.Settings.Default.Save();
                Properties.Settings.Default.Reload();

                MessageBox.Show(string.Format("Thanks for authenticating, {0}.", accessTokens.ScreenName));
            }
            catch (System.Exception ex) { MessageBox.Show(ex.Message, "melodyMe Error"); }
        }
    }
}
